<!doctype html>
<meta charset="utf-8">
<title></title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>

function register_sw(script_url, scope_str='') {
  var registration = navigator.serviceWorker.register(script_url, {'scope': scope_str });
  return registration;
}

test(function (){
  assert_true('serviceWorker' in navigator);
}, "Test: Asserts ServiceWorkerContainer in Navigator");

promise_test(function() {
  return register_sw('resources/sw.js').then(function(sw_reg) {
    assert_equals(sw_reg.active.scriptURL, location.href.replace("service-worker-registration.https.html", "resources/sw.js"));
  });
}, "Test: Active Service Worker ScriptURL property");

promise_test(function() {
  return register_sw('resources/sw.js').then(function(sw_reg) {
    assert_equals(sw_reg.installing.scriptURL, location.href.replace("service-worker-registration.https.html", "resources/sw.js"));
  });
}, "Test: Installing Service Worker ScriptURL property");

promise_test(function() {
  return register_sw('sw.js').then(function(sw_reg) {
    assert_class_string(sw_reg, "ServiceWorkerRegistration");
    assert_class_string(sw_reg.active, "ServiceWorker");
    assert_class_string(navigator.serviceWorker.controller, "ServiceWorker");
  });
}, "Test: Asserts Active Service Worker and its Registration");

promise_test(function() {
  return register_sw('sw.js').then(function(sw_reg) {
    assert_class_string(sw_reg, "ServiceWorkerRegistration");
    assert_class_string(sw_reg.installing, "ServiceWorker");
  });
}, "Test: Asserts Installing Service Worker and its Registration");

promise_test(function() {
  return register_sw('resources/sw.js', './').then(function(sw_reg) {
    assert_equals(sw_reg.scope, location.href.replace("service-worker-registration.https.html", ""));
  });
}, "Test: Service Worker Registration property scope Url when no scope");

promise_test(function() {
  return register_sw('resources/sw.js', '/some/nested/cache/directory').then(function(sw_reg) {
    var expected_scope_url = location.protocol + '//' + location.host + '/some/nested/cache/directory';
    assert_equals(sw_reg.scope, expected_scope_url);
  });
}, "Test: Service Worker Registration property scope when provided a scope");

promise_test(function (p) {
  return promise_rejects_js(p, TypeError, register_sw('./in%5Csome%5fdir/sw.js'));
}, "Test: Throws Error when Invalid Url Path");

promise_test(function (p) {
  return promise_rejects_js(p, TypeError, register_sw('sw.js', './mal%5fformed/sco%5Cpe/'));
}, "Test: Throws Error when Invalid Scope");

</script>
